extends Control

var Unlocked=Color(1,1,1,1)
var Locked=Color(1,0.3,0.3,1)
var DragColor=Color(1,1,1,0)
var SelectedColor=Color(0,1,0,1)
var is_Selected=false

var is_Locked=false
var CurItemID=null


@export var texture_rect : TextureRect
@export var ammount_label : Label

@export var split_ammount_panel : Panel
@export var	split_ammount_text_edit : TextEdit
@export var split_ammount_button : Button

var data={}

func get_inv_node():
	return get_parent().get_parent().get_parent().get_parent()

func SetNewItem(ItemName:String,ammount:int=1):
	data=JsonItemDb.GetItemByID(ItemName)
	if data!=null:
		data["ammount"]=ammount
		
		#如果数量为1 则数量标签显示字符串为空
		if data["ammount"]==1:
			ammount_label.text=""
		else:
			ammount_label.text=str(data["ammount"])
		
		texture_rect.texture=JsonItemDb.SmartTextureLoad(data)
		
func deep_copy(): #深拷贝 复制节点同时复制data的数据
	var new_node=duplicate()
	new_node.data=data.duplicate()
	return new_node

func _ready() -> void:
	mouse_filter = Control.MOUSE_FILTER_IGNORE 
	$Panel.mouse_filter = Control.MOUSE_FILTER_IGNORE 
	#经测试 这两行必须要有
	#自身忽略鼠标的输入 传给父节点
	
	split_ammount_panel.visible=false

func refresh()->void: #刷新节点 更新材质和数量显示
	if data!=null:
		#如果数量为1 则数量标签显示字符串为空
		if data["ammount"]==1:
			ammount_label.text=""
		else:
			ammount_label.text=str(data["ammount"])
					
		texture_rect.texture=JsonItemDb.SmartTextureLoad(data)


func show_split_ammount_panel():
	split_ammount_panel.visible=true
	split_ammount_text_edit.text=str(int(data["ammount"]-int(data["ammount"]/2)))
	mouse_filter = Control.MOUSE_FILTER_STOP

func shut_split_ammount_panel():
	split_ammount_panel.visible=false
	mouse_filter = Control.MOUSE_FILTER_IGNORE 
	
func _on_button_pressed() -> void:
	var split_num=int(split_ammount_text_edit.text)
	if split_num<int(data["ammount"]) and split_num>=1:
		get_inv_node().nowChooseItem=deep_copy()
		get_inv_node().nowChooseItem.position=get_local_mouse_position()
		get_inv_node().nowChooseItem.scale=Vector2(3.4,3.4)
		
		get_inv_node().nowChooseItem.data["ammount"]=split_num
		data["ammount"]-=split_num
		
		get_inv_node().nowChooseItem.refresh()
		refresh()
		
		get_inv_node().add_child(get_inv_node().nowChooseItem)
		
		
	elif split_num==int(data["ammount"]):
		get_inv_node().nowChooseItem=deep_copy()
		get_inv_node().nowChooseItem.position=get_local_mouse_position()
		get_inv_node().nowChooseItem.scale=Vector2(3.4,3.4)
		get_inv_node().add_child(get_inv_node().nowChooseItem)
		
		queue_free()	
	shut_split_ammount_panel()

#func SetEmptyItem():
	#$Panel/Ammount.text=""
	#
#func LockItem():
	#is_Locked=true
	#$Panel.set_modulate(Locked)
	#
#func UnlockItem():
	#is_Locked=false
	#$Panel.set_modulate(Unlocked)
	#
#func SetAsDrag(ItemID):
	#CurItemID=ItemID
	#$Panel/TextureRect.texture=JsonItemDb.SmartTextureLoad(ItemID)
	#$Panel.set_self_modulate(DragColor)
	#SyncAmmount()
	#
#func RemoveItem(Ammount=null):
	#if Ammount!=null:
		#if Ammount>0:
			#CurItemID["ammount"]-=Ammount
			#SyncAmmount()
			#return
	#else:
		#CurItemID=null
		#$Panel/TextureRect.texture=null
		#$Panel/Ammount.visible=false
		#
#func SetAsSelected():
	#if is_Selected: return
	#is_Selected=true
	#$Panel.set_modulate(SelectedColor)
#
#func SetAsUnselected():
	#if !is_Selected: return
	#is_Selected=false
	#$Panel.set_modulate(Unlocked)
#
#func SyncAmmount():
	#$Panel/Ammount.text=String(CurItemID["ammount"])
	#if CurItemID["ammount"]==1:
		#$Panel/Ammount.visible=false
	#else:
		#$Panel/Ammount.visible=true
#
#func AddItem(ItemID):
	#if CurItemID!=null:
		#CurItemID["ammount"]!=ItemID["ammount"]
		#SyncAmmount()
		#return
	#CurItemID=ItemID
	#$Panel/TextureRect.texture=JsonItemDb.SmartTextureLoad(ItemID)
	#SyncAmmount()
	
	
